from django.core.paginator import Paginator
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Book
from .serializer import BookSerializer
from Publisher.models import Publisher

class BookAdd(APIView):
    def post(self, request):
        name = request.data.get('name')
        status1 = request.data.get('status')
        reader = request.data.get('reader')
        publisher_id=request.data.get('publisher')

        if Book.objects.filter(name=name).exists():
            return Response({'error':'书名不能重复'}, status=status.HTTP_400_BAD_REQUEST)

        if Publisher.objects.filter(id=publisher_id).exists():
            book=Book.objects.create(name=name,status=status1,reader=reader,publisher_id=publisher_id)
            book.save()
            return Response({'succeed':'添加成功'}, status=status.HTTP_200_OK)
        else:
            return Response({'error':'该出版社不存在'}, status=status.HTTP_400_BAD_REQUEST)

class BookList(APIView):
    def get(self, request):
        books = Book.objects.all()
        _page=request.GET.get('_page',1)
        _per_page=request.GET.get('_per_page',5)
        title = request.GET.get('title')
        if title:
            books = books.filter(title__contains=title)

        paginator = Paginator(books, _per_page)
        current_page = paginator.page(_page)
        serializer = BookSerializer(current_page, many=True)
        return Response({
            'data':serializer.data,
            'total':paginator.count,
            'items':paginator.count,
            '_page':_page,
            '_per_page':_per_page,
        })

class BookLend(APIView):
    def put(self, request,id):
        list = Book.objects.get(id=id)
        if list.status == 0:
            list.status = 1
            serializer = BookSerializer(list, request.data)
            try:
                if serializer.is_valid():
                    serializer.save()
                    return Response(status=status.HTTP_200_OK)
            except Exception as e:
                return Response(status=status.HTTP_200_OK)
        else:
            list.status = 0
            serializer = BookSerializer(list, request.data)
            try:
                if serializer.is_valid():
                    serializer.save()
                    return Response(status=status.HTTP_200_OK)
            except Exception as e:
                return Response(status=status.HTTP_200_OK)

class BookBack(APIView):
    def put(self, request,id):
        list = Book.objects.get(id=id)
        if list.status==0:
            list.status = 1
            serializer = BookSerializer(list,request.data)
            try:
                if serializer.is_valid():
                    serializer.save()
                    return Response(status=status.HTTP_200_OK)
            except Exception as e:
                return Response(status=status.HTTP_200_OK)
        else:
            list.status = 0
            serializer = BookSerializer(list, request.data)
            try:
                if serializer.is_valid():
                    serializer.save()
                    return Response(status=status.HTTP_200_OK)
            except Exception as e:
                return Response(status=status.HTTP_200_OK)

